package org.lansir.af.dataview;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import org.lansir.af.database.Database;

/**
 * 数据存储视图，用户过滤从数据库获取的数据
 * @author 蓝智宏
 *
 */
public abstract class DataView {
	public static final String TAG = "DataView";
	// fields
	protected String sQuery = "select * from %s";
	protected Database db = null;
	protected String sTableName = "";
	// 所有数据项
	protected ArrayList<DataViewItem> rows = new ArrayList<DataViewItem>();
	private Calendar calViewStartDate = Calendar.getInstance();
	protected List<DataViewItem> filteredRows = new ArrayList<DataViewItem>();
//	protected Map<CalendarKey, List<DataViewItem>> mapDayRowsForView = new HashMap<CalendarKey, List<DataViewItem>>();

	// methods
	public DataView(Database db) {
		this.db = db;
	}

	protected abstract void addItem(Cursor cr);

	protected abstract void filterDataPrepare(final Calendar calStartDate, final int iDayType);

	protected abstract void filterDataForView(DataViewItem item,
			final Calendar calStartDate, final int iDayType);

	protected abstract void sortView();

	public boolean reloadTable() {
		// get data
		Database.Result result = collectRowsData();
		if (result == Database.Result.Success)
			return true;
		return false;
	}

	public Database.Result collectRowsData() {
		
		Database.Result result = Database.Result.errUnknown;
		rows.clear();
		String sql = String.format(sQuery, sTableName);
		SQLiteDatabase sqldb = db.getSQLiteDb();

		Cursor cr = sqldb.rawQuery(sql, null);
		if (cr == null) {
			result = Database.Result.errCantGetData;
	
		} else {
			result = Database.Result.Success;
			if (cr.getCount() > 0) {
				cr.moveToFirst();
				while (!cr.isAfterLast()) {
					try {
						addItem(cr);
						
					} catch (Exception e) {
						Log.v(TAG, "errCantGetDataFromTable");
						return Database.Result.errCantGetDataFromTable;
					}
					cr.moveToNext();
				}
				sortView();
//				Log.v(TAG, "data count:" + cr.getCount());
			} 
			
			
		}
		cr.close();

		return result;
	}
	
	

	public DataViewItem getRow(int index) {
		try {
			DataViewItem values = rows.get(index);

			return values;
		} catch (IndexOutOfBoundsException e) {
		}
		return null;
	}
	
	public DataViewItem getRowForView(int index) {
		try {
			DataViewItem values = filteredRows.get(index);

			return values;
		} catch (IndexOutOfBoundsException e) {
		}
		return null;
	}
	
	public List<DataViewItem> getRowsForTotal(){
		return this.rows;
	}
	
	public List<DataViewItem> getRowsForView(){
		return this.filteredRows;
	}

	public int getRowsCountForView(){
		return this.filteredRows.size();
	}
	
	public int getRowsCountTotal() {
		return rows.size();
	}

	public void filterData(final Calendar calStartDate, final int iDayType) {
		calViewStartDate.setTimeInMillis(calStartDate.getTimeInMillis());
		this.filteredRows.clear();
		// calViewStartDate.setFirstDayOfWeek(prefs.iFirstDayOfWeek);
		filterDataPrepare(calViewStartDate ,iDayType);
		
		for (int i = 0; i < rows.size(); i++) {
			DataViewItem item = rows.get(i);
			filterDataForView(item, calViewStartDate, iDayType);
		}
		sortView();
	}

}
